/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 19 Jan 2011, 11:15:54 AM
 */
package za.co.pas.spaceandtime.view;

import za.co.pas.business.abstracts.AbstractView;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import za.co.pas.business.constant.Constant;
import za.co.pas.spaceandtime.component.StatusPanel;
import za.co.pas.business.controller.CompanyDetailController;
import za.co.pas.business.message.MessageObj;
import za.co.pas.business.data.Company;
import za.co.pas.business.data.UserLoginDetails;
import za.co.pas.business.model.ext.CompanyDetailModel;
import za.co.pas.business.model.CountryDataModel;
import za.co.pas.business.model.ext.UserDetailModel;
import za.co.pas.business.view.model.list.UserDetailListModel;
import za.co.pas.business.view.model.list.UserListModel;
import za.co.pas.business.mvc.BusinessMVC;
import za.co.pas.spaceandtime.constant.STConstant;
import za.co.pas.spaceandtime.controller.BuildingDetailController;
import za.co.pas.spaceandtime.mvc.SpaceAndTimeMVC;
import za.co.pas.spaceandtime.view.tabbedpane.TabbedPane;

/**
 *
 * @author alabuschagne
 */
public class MainFrame extends JFrame implements AbstractView, PropertyChangeListener
{
    //MVC Controllers
    private CompanyDetailController companyDetailController;
    private BuildingDetailController buildingDetailController;
    //MVC Models
    private CountryDataModel countryDataModel;
    private UserDetailModel userDetailModel;
    private CompanyDetailModel companyDetailModel;
//    private AuditDataModel auditDataModel;
    //SWING Panes
    private TabbedPane mainTabbedPane;
    //Panels
    private StatusPanel statusPanel;
    //SWING List Models
    private UserListModel userListModel = new UserListModel();
    private UserDetailListModel userDetailListModel = new UserDetailListModel();
    
    private UserLoginDetails currentUser;
    private static Logger logger = Logger.getLogger(MainFrame.class);
    
    private static final String TITLE = "Space And Time";
    private static final String VER = "0.0.0";

    private MainFrame()
    {
    }

    /** Creates new form MainFrame */
    public MainFrame(UserLoginDetails userLoginDetails)
    {
        currentUser = userLoginDetails;

        initMVC();
        //auditDataModel = new AuditDataModel();
        //once off
        userDetailModel.setCurrentUser(currentUser);
        initComponents();
        ownInit();
        setTitle(TITLE);
    }

    private void initMVC()
    {
        STConstant.stMVC.addPropertyChangeListener(this);
        while(!STConstant.stMVC.isInit())
        {
            try
            {
                //sleep a bit
                Thread.sleep(100);
            }
            catch (InterruptedException ex)
            {
                logger.error(ex,ex);
            }
        }
        countryDataModel = (CountryDataModel) Constant.mvc.getModel(BusinessMVC.COUNTRY_DATA_MODEL);
        userDetailModel = (UserDetailModel) Constant.mvc.getModel(BusinessMVC.USER_DETAIL_MODEL);
        companyDetailModel = (CompanyDetailModel) Constant.mvc.getModel(BusinessMVC.COMPANY_DETAIL_MODEL);
        buildingDetailController =(BuildingDetailController) STConstant.stMVC.getController(SpaceAndTimeMVC.BUILDING_DETAIL_CONTROLLER);
        companyDetailController = (CompanyDetailController) Constant.mvc.getController(BusinessMVC.COMPANY_DETAIL_CONTROLLER);
        companyDetailController.addView(this);
    }

    /**
     * @TODO Check user access!
     */
    private void ownInit()
    {
        statusPanel = new StatusPanel();
        mainTabbedPane = new TabbedPane(companyDetailController, buildingDetailController);
        mainTabbedPane.init();
        getContentPane().add(mainTabbedPane, BorderLayout.CENTER);
        getContentPane().add(statusPanel, BorderLayout.SOUTH);
        this.pack();
        //TODO
        loadCompanyList();
    }

    private void loadCompanyList()
    {
        if(Constant.mvc.isInit())
        {
            companyDetailController.requestCompanyList(true);
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        companyMenu = new javax.swing.JMenu();
        systemNewCompanyMenuItem = new javax.swing.JMenuItem();
        systemEditCompanyMenuItem = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu1 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        companyMenu.setText("Company");

        systemNewCompanyMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        systemNewCompanyMenuItem.setText("New company");
        systemNewCompanyMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                systemNewCompanyMenuItemActionPerformed(evt);
            }
        });
        companyMenu.add(systemNewCompanyMenuItem);

        systemEditCompanyMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        systemEditCompanyMenuItem.setText("Open company...");
        companyMenu.add(systemEditCompanyMenuItem);

        jMenuBar1.add(companyMenu);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jMenu1.setText("jMenu1");
        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //Check to be deleted//check to be deleted//check to be deleted
    private void systemNewCompanyMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_systemNewCompanyMenuItemActionPerformed
    {//GEN-HEADEREND:event_systemNewCompanyMenuItemActionPerformed

        companyDetailController.requestMakeNewCompany();
        //companyDetailController.makeNewCompany();

    }//GEN-LAST:event_systemNewCompanyMenuItemActionPerformed
    //Checked
    //Checked
    //Checked to delete
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu companyMenu;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem systemEditCompanyMenuItem;
    private javax.swing.JMenuItem systemNewCompanyMenuItem;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt)
    {
        if (evt.getPropertyName().compareTo(MessageObj.COMPANY_DETAILS_CHANGED) == 0)
        {
            if ((evt.getNewValue() != null) && (evt.getNewValue() instanceof Company))
            {
                Company sendNewCompany = (Company) evt.getNewValue();
                if ((sendNewCompany.getName() == null) || (sendNewCompany.getName().length() == 0))
                {
                    setTitle(TITLE + " - [NEW]");
                }
                else
                {
                    setTitle(TITLE + " - " + sendNewCompany.getName());
                }
            }
        }
        else if (evt.getPropertyName().compareTo(MessageObj.COMPANY_LIST_CHANGED) == 0)
        {

        }
        else if (evt.getPropertyName().compareTo(MessageObj.COMPANY_DETAILS_SAVED) == 0)
        {
            StringBuilder sb = new StringBuilder("Company details saved");
            Company sendNewCompany = (Company) evt.getNewValue();
            if(sendNewCompany != null)
            {
                sb.append(" for:\n");
                sb.append(sendNewCompany.getName());
            }
            sb.append(".");
            JOptionPane.showMessageDialog(this, sb.toString(), "Company Details Saved", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt)
    {
        logger.info("MainFrame not handling: " + evt.getPropertyName());
    }

    @Override
    public boolean isDirty()
    {
        return false;//for now?
    }

    @Override
    public String getExtraTitle()
    {
        return null;
    }
}
